package com.opennews.openplatform.controller;

import com.opennews.openplatform.dto.authentication.UpdateEnabledUserActiveTokenDto;
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse;
import com.opennews.openplatform.myspringbootcore.dto.IdentityDto;
import com.opennews.openplatform.myspringbootmvccore.helper.HttpResponseHelper;
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor;
import com.opennews.openplatform.service.UserActiveTokenService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/user-active-token")
public class UserActiveTokenController {
    private final UserActiveTokenService userActiveTokenService;
    private final RequestIdentityExtractor requestIdentityExtractor;
    private final HttpResponseHelper httpResponseHelper;

    @GetMapping("/query-device-by-username")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> queryDeviceByUsername() {
        String username = requestIdentityExtractor.getClaims().username;
        List<Map<String, Object>> result = userActiveTokenService.queryDeviceByUsername(username);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @PostMapping("/update-enabled")
    public ResponseEntity<ApiResponse<Object>> updateEnabled(@Valid @RequestBody UpdateEnabledUserActiveTokenDto data) {
        String id = data.getId();
        boolean enabled = data.getEnabled();
        String username = requestIdentityExtractor.getClaims().username;
        userActiveTokenService.updateEnabled(id, username, enabled);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/delete")
    public ResponseEntity<ApiResponse<Object>> delete(@Valid @RequestBody IdentityDto data) {
        String id = data.getId();
        String username = requestIdentityExtractor.getClaims().username;
        userActiveTokenService.delete(id, username);

        return httpResponseHelper.getSuccessResponse();
    }
}
